package bdz.projects.atwww.electrolux.ui
{
	import bdz.projects.atwww.electrolux.events.ELUIEvent;
	import bdz.projects.atwww.electrolux.model.core.ELOption;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	public class ELOptionItem extends MovieClip {
		
		protected var _currentStep:Number;
		protected var _maxSteps:Number;
		protected var _selections:Array;
		protected var _optionRef:ELOption;
		protected var _lastSelected:MovieClip;
		public function ELOptionItem(ar:AbstractRestriction) {
			
			super();
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		protected function initializeComponent(evt:Event):void{
			
		}
		protected function getAbstract():AbstractRestriction{
			return new AbstractRestriction();
		}
		
		protected function checkValidCurrentStep(up:Boolean):void{
			if(_optionRef.selections[_currentStep].enabled){
				return;
			}else{
				if(up){
					if(_currentStep+1 <= _maxSteps){
						_currentStep++;
					}else{
						_currentStep = 0;
					}
				}else{
					if(_currentStep-1 >= 0){
						_currentStep--;
					}else{
						_currentStep = _maxSteps;
					}
				}
				checkValidCurrentStep(up);
			}
		}
		protected function stepUp():void{
			if(isNaN(_currentStep)) return;
			if(_currentStep+1 <= _maxSteps){
				_lastSelected.deselect();
				_currentStep++;
				checkValidCurrentStep(true);
				_lastSelected = _selections[_currentStep].select();
			}else{
				_lastSelected.deselect();
				_currentStep = 0;
				_lastSelected = _selections[_currentStep].select();
			}
			_optionRef.selected = _optionRef.selections[_currentStep].key;
			dispatchEvent(new ELUIEvent(ELUIEvent.OPTION_CHANGED, _optionRef));
		}
		protected function stepDown():void{
			
			if(isNaN(_currentStep)) return;
			if(_currentStep-1 >= 0){
				_lastSelected.deselect();
				_currentStep--;
				checkValidCurrentStep(false);
				_lastSelected = _selections[_currentStep].select();
			}else{
				_lastSelected.deselect();
				_currentStep = _maxSteps;
				_lastSelected = _selections[_currentStep].select();
			}
			_optionRef.selected = _optionRef.selections[_currentStep].key;
			dispatchEvent(new ELUIEvent(ELUIEvent.OPTION_CHANGED, _optionRef));
		}
		public function set option(o:ELOption):void{
			_optionRef = o;
			_selections = new Array();
			
			_maxSteps = _optionRef.selections.length-1;
		}
		public function get option():ELOption{
			return _optionRef;
		}
		
		
		
	}
}
internal class AbstractRestriction {}